<?
/*

	functions used in AJAX calls from the page
	
*/

require_once("config.php");
$blogRoot="../".$blogRoot;
require_once($blogRoot."functions.php");



/* Handle updating of services */
if (isset($_GET["updService"])) {
	$configFile = $blogRoot."settings/services.txt";
	
	if ($_GET["updService"]=="0") {
			// refresh all services

				// read contents
				clearstatcache();
				$open3 = fopen($configFile, "rb");
					if(filesize($configFile)!="0") $contents = fread($open3, filesize($configFile));
				fclose($open3);
				
				// display services
				if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
				else $entrylist=explode("\n",$contents);

				foreach ($entrylist as $item) {
					$entry=explode("|",$item);

					$entry[3]=str_replace(CHR(13),'',$entry[3]);
					$entry[3]=str_replace(CHR(10),'',$entry[3]);
					
					if (function_exists($entry[1]."_download")) call_user_func($entry[1]."_download",$entry[0],$entry[3]);
				}

	} else {
		// read contents
		clearstatcache();
		$open3 = fopen($configFile, "rb");
			if(filesize($configFile)!="0") $contents = fread($open3, filesize($configFile));
		fclose($open3);
		
		// display services
		if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
		else $entrylist=explode("\n",$contents);

		$targetService=$_GET['updService'];
		
		foreach ($entrylist as $item) {
			$entry=explode("|",$item);
			
			//if ($entry[0]."|".$entry[1]==$targetService) {
			if ($entry[0]==$targetService) {
				$entry[3]=str_replace(CHR(13),'',$entry[3]);
				$entry[3]=str_replace(CHR(10),'',$entry[3]);
				
				if (function_exists($entry[1]."_download")) call_user_func($entry[1]."_download",$entry[0],$entry[3]);
				break;
			}
		}
	}

	// archive it all
	archiveTweets();
}




/* Handle adding services */
if (IsSet($_GET['addService'])) {

	if ($_GET['addService']=="0") {
		$category=null;
		
		// display list of supported services
		
		$groups="<div style='width:100%;'>";
		
		foreach ($pluginServices as $key=>$value) {
			if ($key!="puddle") {
				if ($category!=$value) {
					if ($category) $groups.="</div>"; //print "<br/>";
					$category=$value;
					//print "<strong>".$category."</strong><br/>";
					$groups.="<a id='tabMenu' href='#' onClick='swap(\"tab".$category."\"); return false;'>".$category."</a>";
					$groups.="<div id='tab".$category."' style='display:none; padding-left: 25px;'>";
				}
				$groups.="<img src='".serviceIcon($key)."'> ";
				$groups.="<a href='#' onClick='addService(\"".$key."\");' return;'>".$key."</a><br/>";
			}
		}
		$groups.="</div>";
		
		print $groups;
		return;
	}
	
	
	$serviceID=$_GET['addService'];
	
	print "<form action='#' method='post'>";
		print "<input type='hidden' name='addservice' value='".$_GET['addService']."'>";
		if (function_exists($serviceID."_form")) call_user_func($serviceID."_form","");
		else {
			print "Username: <input type='text' name='username'>";
			print " <input type='submit' name='add' value='add'>";
		}
	print "</form>";
	
}



/* Delete a service */
if (isset($_GET["delService"])) {

	$delentries=$_GET["delentries"];
		
	$configFile = $blogRoot."settings/services.txt";
	
		// read contents
		clearstatcache();
		$open3 = fopen($configFile, "rb");
			if(filesize($configFile)!="0") $contents = fread($open3, filesize($configFile));
		fclose($open3);
		
		// process services
		if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
		else $entrylist=explode("\n",$contents);

		$targetService=$_GET['delService'];

		$open3 = fopen($configFile, "w");
		$first=true;
		foreach ($entrylist as $item) {
			$entry=explode("|",$item);
			
			//if ($entry[0]."|".$entry[1]!=$targetService) {
			if ($entry[0]!=$targetService) {
				if (!$first) fwrite($open3,"\r\n");
				fwrite($open3,$item);
				$first=false;
			}
		}
		
		fclose($open3);
		
		print "deleted service <br/>";

		// also delete entries from this service?
		if ($delentries) {
			clearstatcache();
			$handle=opendir($blogRoot."txts/"); 

			// process all archive files, remove everything from this service
			while ($file = readdir($handle)) { 
				if (substr($file,0,1)!="." && substr($file,0,1)!="_" && (filesize($blogRoot."txts/".$file)!="0")) {
					$file=$blogRoot."txts/".$file;
					
					$open2=fopen($file,"rb");
						$contents = fread($open2, filesize($file));
					fclose($open2);
					
					if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
					else $entrylist=explode("\n",$contents);
					
					// now we save all others back to the file, skipping the ones from this service
					$open1 = fopen($file, "w");
					$first=true;
					
					foreach ($entrylist as $entry) {
						if (trim($entry)!="") {
							$item=explode("|",$entry);
							if ($item[1]!=$targetService) {
								if (!$first) fwrite($open1,"\r\n");
								fwrite($open1,$entry);
								$first=false;
							}
						}
					}					
					
					fclose($open1);
				}
			}
			
			print "deleted imported items.<br/>";
		
		}
	
}










/* Handle adding plugins */
if (IsSet($_GET['addPlugin'])) {

	if ($_GET['addPlugin']=="0") {
		// display list of supported services
		foreach ($plugins as $key=>$value) {
			if ($value=="sidebar") {
				$shortName=call_user_func($key."_pluginName");			
				print "<a href='#' onClick='addPlugin(\"".$key."\");' return;'>".$shortName."</a><br/>";
				print " -<i>".call_user_func($key."_pluginDescription")."</i>";
				print "<br/>";
			}
		}
		
		return;
	}
	
	
	$service=$_GET['addPlugin'];
	
	print "<form action='#' method='post'>";
		print "<input type='hidden' name='addPlugin' value='".$_GET['addPlugin']."'>";
		
		print call_user_func($service."_optionForm");

		print "<span style='float:right;'>";
		print "<input type='submit' name='submit' value='Save'>";
		print "</span>";

	print "</form>";
	
}




/* Handle adding plugins */
if (IsSet($_GET['updPlugin'])) {

	$serviceidx=$_GET['updPlugin'];
	$servicetype=$_GET['st'];

	$configFile = $blogRoot."settings/".$servicetype.".txt";
	
	$load = @file($configFile);
	while(count($load)) {
		$line=array_shift($load);
		$info=explode("|",$line);
		
		if ($info[0]==$serviceidx) {
			print "<form action='#' method='post'>";
				print "<input type='hidden' name='updPlugin' value='".$serviceidx."'>";
				print "<input type='hidden' name='updType' value='".$info[1]."'>";
				
				print call_user_func($info[1]."_optionForm",$line);

				print "<span style='float:right;'>";
				print "<input type='submit' name='submit' value='Save'>";
				print "</span>";

			print "</form>";
		}
	}

}




/* Delete a plugin */
if (isset($_GET["delPlugin"])) {

	$servicetype=$_GET["st"];
		
	$configFile = $blogRoot."settings/".$servicetype.".txt";
	
		// read contents
		clearstatcache();
		$open3 = fopen($configFile, "rb");
			if(filesize($configFile)!="0") $contents = fread($open3, filesize($configFile));
		fclose($open3);
		
		// process services
		if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
		else $entrylist=explode("\n",$contents);

		$targetService=$_GET['delPlugin'];

		$open3 = fopen($configFile, "w");
		$first=true;
		foreach ($entrylist as $item) {
			$entry=explode("|",$item);
			
			//if ($entry[0]."|".$entry[1]!=$targetService) {
			if ($entry[0]!=$targetService) {
				if (!$first) fwrite($open3,"\r\n");
				fwrite($open3,$item);
				$first=false;
			}
		}
		
		fclose($open3);
		
		print "removed plugin <br/>";
}



/* Handle adding plugins */
if (IsSet($_GET['sortPlugins'])) {
	$sortorder=$_GET['sortPlugins'];
	
	$sortorder=str_replace("node","",$sortorder);



		$configFile = $blogRoot."settings/sidebar.txt";
		
	
		// file exists?
		if(file_exists($configFile)) {
			if(!(is_writable($configFile))) {
				print "<strong>the settings file \"".$configFile."\" is not writeable</strong>";
				return;
			}
		} else {
			// create the file :)
			$create = touch($configFile);
			chmod($configFile,0777);
			if(!($create)) {
				print "<strong>the settings file \"".$configFile."\" could not be made</strong>";
				return;
			}
		}


		// read contents
		clearstatcache();
		$open3 = fopen($configFile, "rb");
			if(filesize($configFile)!="0") $contents = fread($open3, filesize($configFile));
		fclose($open3);
		
		// display services
		if (strpos($contents,"\r\n")) $entrylist=explode("\r\n",$contents);
		else $entrylist=explode("\n",$contents);
		
		$open1=fopen($configFile,"w"); 
		$order=explode(",",$sortorder);
		
		$first=true;
		foreach($order as $indx) {
				$indx--;
				//print "S #".$indx.":".$entrylist[$indx]."<Br/>";
				
				if (!$first) fwrite($open1,"\r\n");
				
				fwrite($open1,$entrylist[$indx]);
				$first=false;
		}
		
		fclose($open1);
		
}



if (IsSet($_GET["hideTweet"])) {
	hideTweet($_GET['hideTweet'],true);
	print "Post is now hidden";
}


if (IsSet($_GET["showTweet"])) {
	hideTweet($_GET['showTweet'],false);
	print "Post is now visible";
}


?>